package com.onlinetickets;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.onlinetickets.dao.UserDao;
import com.onlinetickets.util.ExitApplication;

public class LoginActivity extends Activity {
    private EditText etName;
    private EditText etPass;
    private Button logins;
    private Button register;
    private Button moreInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        ExitApplication.getInstance().addActivity(this);
        setViews();
        setListener();
    }

    private void setViews() {
        etName = (EditText) findViewById(R.id.etUsername);
        etPass = (EditText) findViewById(R.id.etPwd);
        logins = (Button) findViewById(R.id.btLogin);
        register = (Button) findViewById(R.id.btRegist);
        moreInfo = (Button) findViewById(R.id.textView_moreFun);
    }

    private void setListener() {
        register.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {


                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });

        moreInfo.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(LoginActivity.this, MoreActivity.class);
                startActivity(intent);
            }
        });

        logins.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                String username = etName.getText().toString();
                String password = etPass.getText().toString();
                if (!username.isEmpty() && !password.isEmpty()) {
                    UserDao uDao = new UserDao(LoginActivity.this);
                    boolean flag = uDao.checkLogin(username, password);
                    if (flag) {
                        Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                        SharedPreferences.Editor editor = getSharedPreferences("checkLogin", MODE_PRIVATE).edit();
                        editor.putString("username", username);
                        editor.putString("password", password);
                        editor.putBoolean("login", true);
                        editor.commit();
                        Intent intent = getIntent();
                        intent.setClass(LoginActivity.this, More2Activity.class);
                        intent.putExtra("username", username);
                        startActivity(intent);
                    } else {
                        Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(LoginActivity.this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}